package site.marqstree.latte.ec.main.personal.order;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import site.marqstree.latte.core.delegates.LatteDelegate;
import site.marqstree.latte.core.net.RestClient;
import site.marqstree.latte.core.net.callback.ISuccess;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ec.R2;
import site.marqstree.latte.ec.main.personal.PersonalDelegate;
import site.marqstree.latte.ec.profile.AppProfile;
import site.marqstree.latte.ui.recycler.MultipleItemEntity;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.personal.order
 * 文件名: OrderListDelegate
 * 创建者: marqstree
 * 创建时间: 2020/2/3 16:57
 * 描述: TODO
 */
public class OrderListDelegate extends LatteDelegate {

    private String mType = null;
    final OrderListAdapter mAdapter = new OrderListAdapter();

    @BindView(R2.id.rv_order_list)
    RecyclerView mRecyclerView;

    @Override
    public Object setLayout() {
        return R.layout.delegate_order_list;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        mType = args.getString(PersonalDelegate.ORDER_TYPE);
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initRecyclerView();
    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        RestClient.builder()
                .loader(getContext())
                .url(AppProfile.PERSONAL_GOOD_DATA)
                .params("type", mType)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        final List<MultipleItemEntity> data =
                                new OrderListDataConverter().setJsonData(response).convert();
                        mAdapter.setNewData(data);
                    }
                })
                .build()
                .get();
    }

    private void initRecyclerView(){
        final LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(manager);

        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new OrderListItemClickListener(this));
    }
}
